/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler;

import com.funcom.peeler.peelable.*;
import com.funcom.peeler.symbol.Symbol;
import com.jmex.bui.BComponent;
import com.jmex.bui.BStyleSheet;
import com.jmex.bui.provider.ResourceProvider;
import org.w3c.dom.Document;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * Used to load peeled designs into an application
 */
public class BananaPeel {

    private PeelRoot root;
    private PeelStyleSheet stylesheet;
    private Map< String, BComponent> name_map = new HashMap< String, BComponent >();
    private Collection< PeelComponent > top_components = new HashSet< PeelComponent >();

    public BananaPeel( Class _class, ResourceProvider rsrcprov ) {
        this( _class.getResource( _class.getSimpleName() + ".xml" ).getPath() , rsrcprov );

    }

    public BananaPeel( InputStream peelStream, ResourceProvider rsrcprov ) {
        root = new PeelRoot( new PeelWindow(), null );
        Document doc = PeelerIO.readPeeledNodes( peelStream );

        if ( doc != null )
            root.unpeel( doc );

        init();
        root.getStyleSheet().setResourceProvider( rsrcprov );
        stylesheet = root.getStyleSheet();
    }

    public BananaPeel( InputStream peelStream, InputStream styleStream, ResourceProvider rsrcprov ) {
        root = new PeelRoot( new PeelWindow(), null );
        Document doc = PeelerIO.readPeeledNodes( peelStream );

        if ( doc != null ) {
            if ( doc.getDocumentElement().hasAttribute( Symbol.Attr.stylesheet ) ) {
                doc.getDocumentElement().removeAttribute( Symbol.Attr.stylesheet );
            }
            root.unpeel( doc );
        }

        init();
        stylesheet = new PeelStyleSheet( new InputStreamReader( styleStream ), rsrcprov );
    }

    public BananaPeel( String filename, ResourceProvider rsrcprov ) {
        root = new PeelRoot( new PeelWindow(), null);
        Document doc = PeelerIO.readPeeledNodes( filename );
        root.unpeel( doc );

        init();
        root.getStyleSheet().setResourceProvider( rsrcprov );
        stylesheet = root.getStyleSheet();
    }

    private void init() {

        for ( PeelComponent component : root.getRootWindow().getChildren() ) {
            top_components.add( component );
        }

        root.getRootWindow().removeAllChildren();

        gather_names();
    }

    private static String getResourcePath( Class _class ) {

        try {
            String path = _class.getResource( _class.getSimpleName() + ".xml" ).getPath();
            return path;
        }
        catch ( Exception e ) {
            return null;
        }
    }

    public BComponent getComponent( String name ) {
        return name_map.get( name );
    }

    public BStyleSheet getStyleSheet() {
        return stylesheet;
    }

    public Collection< BComponent > getTopComponents() {

        Collection< BComponent > bComponents = new HashSet< BComponent >();

        for ( PeelComponent component : top_components ) {
            bComponents.add( component.getComponent() );
        }

        return bComponents;
    }

    private void gather_names() {

        for ( PeelComponent component : top_components ) {
            gather_names( component );
        }
    }

    private void gather_names( PeelComponent component ) {

        if ( component.getComponent().getName() != null ) {
            name_map.put( component.getComponent().getName(), component.getComponent() );
        }

        if ( component instanceof PeelContainer) {

            for ( PeelComponent child : ((PeelContainer)component).getChildren() ) {
                gather_names( child );
            }
        }
    }
}
